package com.kaibes.module.file.local.db;

import java.math.BigInteger;
import java.time.LocalDateTime;

import org.springframework.stereotype.Service;

import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.orm.curd.UpdateBuilder;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class FileInfoService extends ResponseServiceImpl<FileInfo> {
    
    public boolean hasByMD5(String md5) {
        return count(QueryBuilder.newBuilder().eq("md5", md5).build()) > 0;
    }
    
    public boolean hasByID(BigInteger id) {
        return count(QueryBuilder.newBuilder().eq("id", id).build()) > 0;
    }
    
    public FileInfo getByMD5(String md5) {
        return get(QueryBuilder.newBuilder().eq("md5", md5).build());
    }
    
    public int addCount(BigInteger id) {
        return getMapper().update(UpdateBuilder.newBuilder().setValue("access_time", LocalDateTime.now()).setValue("access_count+", 1)
                .addQuery(QueryBuilder.newBuilder().eq("id", id).build()).build());
    }
    
}
